#include "menu.h"

//运行日志
void menu_log(String str)
{
    Serial.println(str);
}

//按键初始化
void keyInit(int up = KE_UP, int down = KEY_DOWN, int left = KEY_LEFT, int right = KEY_RIGHT)
{
    pinMode(up, INPUT_PULLDOWN);
    pinMode(down, INPUT_PULLDOWN);
    pinMode(left, INPUT_PULLDOWN);
    pinMode(right, INPUT_PULLDOWN);
}

//按键扫描
KEY_enum keyScan()
{
    uint8_t time = 0;
    if (digitalRead(KE_UP) == KEY_PUTDOWN)
    {
        delay(10);
        if (digitalRead(KE_UP) == KEY_PUTDOWN)
        {
            while (digitalRead(KE_UP) == KEY_PUTDOWN && time < 100)
            {
                delay(1);
                time++;
            }
#ifdef MENU_DEBUG_LOG
            menu_log("key up putdown");
#endif
            return KE_UP;
        }
    }
    if (digitalRead(KEY_RIGHT) == KEY_PUTDOWN)
    {
        delay(10);
        if (digitalRead(KEY_RIGHT) == KEY_PUTDOWN)
        {
            while (digitalRead(KEY_RIGHT) == KEY_PUTDOWN && time < 100)
            {
                delay(1);
                time++;
            }
            // menu_log("key right putdown");
            return KEY_RIGHT;
        }
    }
    if (digitalRead(KEY_LEFT) == KEY_PUTDOWN)
    {
        delay(10);
        if (digitalRead(KEY_LEFT) == KEY_PUTDOWN)
        {
            while (digitalRead(KEY_LEFT) == KEY_PUTDOWN && time < 100)
            {
                delay(1);
                time++;
            }
            // menu_log("key left putdown");
            return KEY_LEFT;
        }
    }
    if (digitalRead(KEY_DOWN) == KEY_PUTDOWN)
    {
        delay(10);
        if (digitalRead(KEY_DOWN) == KEY_PUTDOWN)
        {
            while (digitalRead(KEY_DOWN) == KEY_PUTDOWN && time < 100)
            {
                delay(1);
                time++;
            }
            // menu_log("key down putdown");
            return KEY_DOWN;
        }
    }
    return KEY_NONE;
}


